package com.mc.cloud.mall.categoryProduct.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.cloud.mall.categoryProduct.common.ProductConstant;
import com.mc.cloud.mall.categoryProduct.model.mapper.ProductMapper;
import com.mc.cloud.mall.categoryProduct.model.pojo.Product;
import com.mc.cloud.mall.categoryProduct.model.vo.AddProduct;
import com.mc.cloud.mall.common.common.Constant;
import com.mc.cloud.mall.common.common.MallRestResponse;
import com.mc.cloud.mall.common.exception.MallException;
import com.mc.cloud.mall.common.exception.MallExceptionEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName AdminProductServiceImpl
 * @Author MC
 * @Date 2020/11/5 9:44
 * @Version 1.0
 **/
@Service
public class AdminProductServiceImpl implements AdminProductService {
    
    @Autowired
    ProductMapper productMapper;
    
    @Value("${file.upload.ip}")
    String ip;
    
    @Value("${file.upload.port}")
    Integer port;
    
    @Override
    public void addProduct(AddProduct addProduct){
    
        //查询是否存在这个商品
        Product productOld = productMapper.selectByName(addProduct.getName());
        if (productOld!=null){
            throw new MallException(MallExceptionEnum.NAME_EXISTED);
        }
        Product product = new Product();
        BeanUtils.copyProperties(addProduct,product);
        int count = productMapper.insertSelective(product);
        if (count ==0){
            throw new MallException(MallExceptionEnum.CREATE_FAILED);
        }
        
    }
    
    //上传图片
    @Override
    public MallRestResponse upload(HttpServletRequest request, MultipartFile file){
        String filename = file.getOriginalFilename();
        String suffixName = filename.substring(filename.lastIndexOf("."));
        //生成文件名
        String newFileName = UUID.randomUUID().toString() + suffixName;
        //生成文件夹 文件
        File fileDirectory = new File(ProductConstant.FILE_UPLOAD_DIR);
        File destFile = new File(ProductConstant.FILE_UPLOAD_DIR +  newFileName);
        if (!fileDirectory.exists()){
            if (!fileDirectory.mkdir()) {
                throw new MallException(MallExceptionEnum.MKDIR_FAILED);
            }
        }
        //将图片写入到指定的文件中
        try {
            file.transferTo(destFile);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回我们生成的路径
        try {
            return MallRestResponse.success(getHost(new URI(request.getRequestURL()+""))+"/categoryProduct" + "/images/" +newFileName);
        } catch (URISyntaxException e) {
            return MallRestResponse.error(MallExceptionEnum.UPLOAD_FAILED);
        }
    }
    
    public URI getHost(URI uri){
        URI effectiveURI ;
        try {
            effectiveURI = new URI(uri.getScheme(),uri.getUserInfo(),ip,port,null,null,null);
        } catch (URISyntaxException e) {
            effectiveURI = null;
        }
        return effectiveURI;
    }
    
    
    @Override
    public void updateProduct(Product product){
        
        Product productOld = productMapper.selectByName(product.getName());
        if (productOld!=null && !productOld.getId().equals(product.getId())){
            throw new MallException(MallExceptionEnum.NAME_EXISTED);
        }
        int count = productMapper.updateByPrimaryKeySelective(product);
        if (count ==0){
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
    
    }
    
    @Override
    public void deleteProduct(Integer id){
        Product product = productMapper.selectByPrimaryKey(id);
        if (product==null){
            throw new MallException(MallExceptionEnum.PRODUCT_NOT_EXIST);
        }
        int count = productMapper.deleteByPrimaryKey(id);
        if (count ==0){
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
    }
    
    @Override
    public void batchUpdateSellStatus(Integer[] ids, Integer sellStatus){
        
        productMapper.batchUpdateSellStatus(ids, sellStatus);
    }
    
    
    @Override
    public PageInfo productList(Integer pageSize,Integer pageNum){
        PageHelper.startPage(pageNum,pageSize);
        List<Product> products = productMapper.selectListForAdmin();
        PageInfo pageInfo = new PageInfo(products);
        return pageInfo;
    
    }
}
